package com.quanzhi.openapi.host;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class HostInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            HostRequired hostRequired = handlerMethod.getMethodAnnotation(HostRequired.class);

            if (hostRequired != null) {
                String host = request.getHeader("Host");
                System.out.println(request);
                log.warn("该请求的HOST是:" + host);

                request.setAttribute("host", host);
            }
        }
        return true;
    }
}
